<?php
/*


/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/												\
			Subject	:	Class Subject    
\												/

*/
	class Subject
	{
		private $id;
		private $gId;
		private $tId;
		private $sId;
		private $number;
		private $name;
		private $credit;
		private $detail;
		

		function __construct()
		{
			
		}
		
		function getID()
		{
			return $this->id;
		}
		
		function setID($set)
		{
			$this->id = $set;
		}
		
		function getGID()
		{
			return $this->gId;
		}
		
		function setGID($set)
		{
			$this->gId = $set;
		}
		
		function getTID()
		{
			return $this->tId;
		}
		
		function setTID($set)
		{
			$this->tId = $set;
		}
		
		function getSID()
		{
			return $this->sId;
		}
		
		function setSID($set)
		{
			$this->sId = $set;
		}
		
		function getNember()
		{
			return $this->number;
		}
		
		function setNumber($set)
		{
			$this->number = $set;
		}
		
		function getName()
		{
			return $this->name;
		}
		
		function setName($set)
		{
			$this->name = $set;
		}
		
		function getCredit()
		{
			return $this->credit;
		}
		
		function setCredit($set)
		{
			$this->credit = $set;
		}
		
		function getDetail()
		{
			return $this->detail;
		}
		
		function setDetail($set)
		{
			$this->detail = $set;
		}
		
		function setAll($id,$gId,$tId,$sId,$number,$name,$credit,$detail)
		{
			$this->id = $id;
			$this->gId = $gId;
			$this->tId = $tId;
			$this->sId = $sId;
			$this->number = $number;
			$this->name = $name;
			$this->credit = $credit;
			$this->detail = $detail;
		}
		
		function findOne()
		{
			$query = mysql_query('SELECT S_G_id,S_T_id,S_S_id,S_name,S_number,S_credit,S_detail FROM subject WHERE S_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			$this->gId = $arr->S_G_id;
			$this->tId = $arr->S_T_id;
			$this->sId = $arr->S_S_id;
			$this->number = $arr->S_number;
			$this->name = $arr->S_name;
			$this->credit = $arr->S_credit;
			$this->detail = $arr->S_detail;
			return($arr->S_name);
		}
		
		function findOneJSON()
		{
			include('teacher.php');
			$tea = new Teacher;
			include('deparment.php');
			$dep = new Deparment;
			include('group_subject.php');
			$gop = new Group_subject;
			$sub = new Subject;
			$data_array = array();
			
			$query = mysql_query('SELECT S_G_id,S_T_id,S_S_id,S_name,S_number,S_credit,S_detail FROM subject WHERE S_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
			$arr = mysql_fetch_object($query);
			
			$data_array[1]['gid'] = $arr->S_G_id;
			$data_array[1]['name'] = $arr->S_name;
			$data_array[1]['number'] = $arr->S_number;
			$data_array[1]['credit'] = $arr->S_credit;
			$tea->setID($arr->S_T_id);
			$data_array[1]['tid'] = $tea->getID();
			$data_array[1]['tName'] = $tea->findOne();
			$gop->setID($arr->S_G_id);
			$data_array[1]['gid'] = $gop->getID();
			$data_array[1]['gName'] = $gop->findOne();
			$dep->setID($gop->getDID());
			$data_array[1]['did'] = $dep->getID();
			$data_array[1]['dName'] = $dep->findOne();
			if($arr->S_S_id != 0)
			{
				$sub->setID($arr->S_S_id);
				$data_array[1]['sid'] = $sub->getID();
				$data_array[1]['sName'] = $sub->findOne();
			}else
			{
				$data_array[1]['sid'] = "0";
				$data_array[1]['sName'] ="NONE";	
			}
			$data_array[1]['detail'] = $arr->S_detail;
			
			return json_encode($data_array);
		}
		
		function findAll()
		{
			include('teacher.php');
			$tea = new Teacher;
			include('deparment.php');
			$dep = new Deparment;
			include('group_subject.php');
			$gop = new Group_subject;
			$sub = new Subject;
			
			$query = mysql_query('SELECT S_id,S_G_id,S_T_id,S_S_id,S_name,S_number,S_credit,S_detail FROM subject')or die(mysql_error());
			$data_array = array();
			$conut = 1;
			while($arr = mysql_fetch_object($query))
			{
				$data_array[$conut]['id'] = $arr->S_id;
				$data_array[$conut]['gid'] = $arr->S_G_id;
				$data_array[$conut]['name'] = $arr->S_name;
				$data_array[$conut]['number'] = $arr->S_number;
				$data_array[$conut]['credit'] = $arr->S_credit;
				$tea->setID($arr->S_T_id);
				$data_array[$conut]['tid'] = $tea->getID();
				$data_array[$conut]['tName'] = $tea->findOne();
				$gop->setID($arr->S_G_id);
				$data_array[$conut]['gid'] = $gop->getID();
				$data_array[$conut]['gName'] = $gop->findOne();
				$dep->setID($gop->getDID());
				$data_array[$conut]['did'] = $dep->getID();
				$data_array[$conut]['dName'] = $dep->findOne();
				if($arr->S_S_id != 0)
				{
					$sub->setID($arr->S_S_id);
					$data_array[$conut]['sid'] = $sub->getID();
					$data_array[$conut]['sName'] = $sub->findOne();
				}else
				{
					$data_array[$conut]['sid'] = "0";
					$data_array[$conut]['sName'] ="NONE";	
				}
				$data_array[$conut]['detail'] = $arr->S_detail;
				$conut++;
			}
			return json_encode($data_array);
		}
		
		function add()
		{
			mysql_query('INSERT INTO subject(S_G_id,S_T_id,S_S_id,S_name,S_number,S_credit,S_detail) VALUES("'.$this->gId.'","'.$this->tId.'","'.$this->sId.'","'.$this->name.'","'.$this->number.'","'.$this->credit.'","'.$this->detail.'")')or die(mysql_error());
		}
		
		function edit()
		{
			mysql_query('UPDATE subject SET S_G_id="'.$this->gId.'",S_T_id="'.$this->tId.'",S_S_id="'.$this->sId.'",S_name="'.$this->name.'",S_number="'.$this->number.'",S_credit="'.$this->credit.'",S_detail="'.$this->detail.'" WHERE S_id = "'.$this->id.'" LIMIT 1')or die(mysql_error());
		}
		
		function del()
		{
			mysql_query('DELETE FROM subject WHERE S_id="'.$this->id.'" LIMIT 1') or die(mysql_error());
		}
		
	}
?>